package com.twt.recruit.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.twt.recruit.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

@Service
public class FileService {

    @Value("${web.upload-path}")
    public String uploadPath;
    public String upload(MultipartFile file) throws IOException {
        String originName = file.getOriginalFilename();
        String type = FileUtil.extName(originName);
        String format = DateUtil.today();
        String fileId = IdUtil.fastSimpleUUID()+ StrUtil.DOT + type;
        if (!FileUtil.exist(uploadPath+format)){
            FileUtil.mkdir(uploadPath+format);
        }
        File uploadFile = new File(uploadPath+format+StrUtil.C_SLASH+fileId);
        file.transferTo(uploadFile);
        return StrUtil.C_SLASH+format+StrUtil.C_SLASH+fileId;
    }
}
